﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class VehicleDataAccess
    {
        public string InsertVehicle(VehicleInfo vehicle)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var ve = db.VehicleTypes.Where(v => v.Model.Equals(vehicle.Model) &&
                v.NumberOfSeat == vehicle.NumberOfSeat).SingleOrDefault();
            if (ve == null)
            {
                VehicleType v = new VehicleType();

                v.Model = vehicle.Model;
                v.NumberOfSeat = vehicle.NumberOfSeat;

                db.VehicleTypes.InsertOnSubmit(v);
                db.SubmitChanges();
                return "Add new vehicle successful";
            }
            else
            {
                return "This vehicle is exist";
            }
            
        }

        public List<VehicleInfo> getListVehicleType()
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var result = from v in db.VehicleTypes
                         orderby v.Model
                         select new
                         VehicleInfo
                         {
                             VehicleID = v.VehicleID,
                             Model = v.Model,
                             NumberOfSeat = Convert.ToInt32(v.NumberOfSeat)
                         };

            return result.ToList();
        }

        public VehicleInfo getVehicleTypeByModel(string model)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var vehicle = db.VehicleTypes.Where(v => v.Model.Equals(model)).SingleOrDefault();
            if (vehicle != null)
            {
                VehicleInfo ve = new VehicleInfo();
                ve.VehicleID = vehicle.VehicleID;
                ve.Model = vehicle.Model;
                ve.NumberOfSeat = Convert.ToInt32(vehicle.NumberOfSeat);
                return ve;
            }
            else
            {
                return null;
            }
        }
    }
}
